﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[amix_StergeDocument]
	-- Add the parameters for the stored procedure here
	@IDDocument int 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	DECLARE @IDMFix int
	DECLARE @IDPrincipal int
	DECLARE @EroareStr varchar(1000)
	DECLARE @NrInv varchar(20)
	DECLARE @TipDoc smallint
	DECLARE @ValSupl decimal(18,2)
	DECLARE @valInv decimal(18,2)
	DECLARE @durAmo int
	DECLARE @durExt int
	DECLARE @IDBK int
	SELECT @IDMFix=[IDMijlocFix],@TipDoc = [IDTipDocument],@ValSupl=[ValoareSuplimentara],
		@valInv = [ValoareInventar],@durAmo=[DurataAmortizare],@durExt=[DurExtindere],@IDBK=[IDBK] FROM [AmixDocumente] WHERE [IDDocument]=@IDDocument

	--verifica daca e mijoc principal
	IF @IDMFix IS NOT NULL
	SELECT @IDPrincipal = [IDMFixPrincipal] FROM [AmixAnsamble]
		WHERE [IDMFixPrincipal] = @IDMFix OR [IDMfixComponenta] = @IDMFix
	
	IF @IDPrincipal IS NOT NULL
	BEGIN
		SELECT @NrInv = [NrInventar] FROM [AmixRegistru] WHERE [IDMijlocFix]=@IDPrincipal
		SET @EroareStr = 'Mijlocul fix cu numarul: ' + @NrInv + ' face parte dintr-un ansamblu si nu poate fi sters!'
		GOTO EROARE1
	END
    
	BEGIN TRAN
	IF @IDMFix IS NOT NULL AND  @TipDoc = 2 --adaugare mijloc fix
	BEGIN
		DELETE FROM [AmixAmortizare] WHERE [IDMijlocFix] = @IDMFix
		IF @@ERROR <>0 GOTO Eroare 
		DELETE FROM [AmixRegistru] WHERE [IDMijlocFix] = @IDMFix 
		IF @@ERROR <>0 GOTO Eroare 
	END		
	
	IF @TipDoc <> 2  AND NOT @IDMFix IS NULL  -- nu e adaugare mijloc fix
	BEGIN
		--- sterge datele actuale si restaireaza bk
		DELETE [AmixAmortizare] WHERE [IDMijlocFix] = @IDMFix
		IF @@ERROR <>0 GOTO Eroare
	
		DELETE [AmixRegistru] WHERE [IDMijlocFix] = @IDMFix
		IF @@ERROR <>0 GOTO Eroare
		
		SET IDENTITY_INSERT [AmixRegistru] ON
		INSERT INTO [AmixRegistru]
           ([IDMijlocFix],[NrInventar],[Denumire],[ContAmortizare],[StocFaptic],[IDClasificare],[DataIntrare],[DataIesire],[CodUnitate],[IDDepartament],[IDStare],[ContImobilizare],[ContCheltuiala],[DurataAmortizare],[RegimAmortizare],[ValoareInventar]
           ,[PunereInFunctiune],[Compus],[Blocat],[IDGestionar],[ContTransfer],[ValoareAmortizata],[LuniAmortizate])
		SELECT [IDMijlocFix],[NrInventar],[Denumire],[ContAmortizare],[StocFaptic],[IDClasificare],[DataIntrare],[DataIesire],[CodUnitate],[IDDepartament],[IDStare],[ContImobilizare],[ContCheltuiala],[DurataAmortizare],[RegimAmortizare],[ValoareInventar]
           ,[PunereInFunctiune],[Compus],[Blocat],[IDGestionar],[ContTransfer],[ValoareAmortizata],[LuniAmortizate] FROM [AmixRegistruBK] a 
		WHERE [IDBK]=@IDBK  
		IF @@ERROR <>0 GOTO Eroare  	
		SET IDENTITY_INSERT [AmixRegistru] OFF


		INSERT INTO [AmixAmortizare] ([IDMijlocFix],[Luna],[An],[ValoareInventar],[ValoareAmortizata],[ValoareRamasa],[AmortizareAn],[AmortizareLuna]
		  ,[DurataNormata],[LuniAmortizate],[Blocat])
		SELECT a.[IDMijlocFix],a.[Luna],a.[An],a.[ValoareInventar],a.[ValoareAmortizata],a.[ValoareRamasa],a.[AmortizareAn],a.[AmortizareLuna]
		  ,a.[DurataNormata],a.[LuniAmortizate],a.[Blocat] FROM [AmixAmortizareBK] a
		WHERE [IDBKRegistru] = @IDBK		

	END	
	
	DELETE FROM [AmixDocumente] WHERE [IDDocument] =@IDDocument
	IF @@ERROR <>0 GOTO Eroare 
	COMMIT
	
	GOTO Gata
	

EROARE:
	ROLLBACK
	RAISERROR ('Eroare la prelucrarea datelor',16,-1)
	GOTO Gata
EROARE1:
	RAISERROR (@EroareStr,16,-1)
	GOTO Gata
EROARE2:
	RAISERROR ('Capitol incorect',16,-1)
	GOTO Gata
	GATA:
	
	
END